﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BBPA.Admin.SYS.Supplier
{
    public partial class List : ListPage
    {
        BLL.V_Supplier bllList = new BLL.V_Supplier();
        BLL.Supplier bll = new BLL.Supplier();
        //
        BLL.PAUser bllUser = new BLL.PAUser();
        protected void Page_Load(object sender, EventArgs e)
        {
            Master.show_formControls = Security.InUserType(new List<EnumHelper.UserType>() { EnumHelper.UserType.Administrator, EnumHelper.UserType.SupplierServer });

            Master.show_btn_add =
                Security.InUserType(new List<EnumHelper.UserType>() { EnumHelper.UserType.Administrator, EnumHelper.UserType.SupplierServer, EnumHelper.UserType.Buyer, EnumHelper.UserType.Store });
            Master.show_btn_bat_del = Master.show_checkbox = Master.show_edit =
                Security.InUserType(new List<EnumHelper.UserType>() { EnumHelper.UserType.Administrator, EnumHelper.UserType.SupplierServer, EnumHelper.UserType.Buyer });
            Master.show_listControls =
                Security.InUserType(new List<EnumHelper.UserType>() { EnumHelper.UserType.Administrator, EnumHelper.UserType.Accountant });
            
            if (!Security.InUserType(new List<EnumHelper.UserType>() { EnumHelper.UserType.Administrator,EnumHelper.UserType.Accountant}))
            {
                th_Account.Visible = false;
                Master.ArrayControlID_noVisible_cph_td.Add("td_Account");
            }
        }
        public override void buildSQL_Page_List_search()
        {
            Master.SearchSQL = "";
            if (search_text_fuzzy.Text != "")
            {
                string fuzzyText = search_text_fuzzy.Text.Replace("　", "|").Replace(" ", "|").Replace("，", "|").Replace(",", "|").Replace("||", "|");
                string fuzzySQL = "";
                foreach (string fuzzyVal in fuzzyText.Split('|'))
                {
                    if (fuzzySQL != "") fuzzySQL += " or ";
                    fuzzySQL += " name like '%" + fuzzyVal + "%' or  number like '%" + fuzzyVal + "%' or  contacts like '%" + fuzzyVal + "%' or  tel like '%" + fuzzyVal + "%' or  mobile like '%" + fuzzyVal + "%'";
                }
                Master.SearchSQL = StringPlus.SQLand(Master.SearchSQL, " ( " + fuzzySQL + ")");
            }
            //if (search_text_fuzzy.Text != "")
            //    Master.SearchSQL = StringPlus.SQLand(Master.SearchSQL, " ( name like '%" + search_text_fuzzy.Text + "%' or  number like '%" + search_text_fuzzy.Text + "%' or  contacts like '%" + search_text_fuzzy.Text + "%' or  tel like '%" + search_text_fuzzy.Text + "%' or  mobile like '%" + search_text_fuzzy.Text + "%')"); ;
            if (search_radio_state.SelectedValue != "")
                Master.SearchSQL = StringPlus.SQLand(Master.SearchSQL, "State=" + search_radio_state.SelectedValue + "");
        }
        public override DataSet get_Page_DataSet(string sql_, string orderby_, int pageFrom_, int pageTo_) { return bllList.GetListByPage(sql_, orderby_, pageFrom_, pageTo_); }
        public override void del_Page_List(string inStr_)
        {
            foreach (string id in inStr_.Split(','))
            {
                int thisID = DataTypePlus.requestInt(id);
                Model.Supplier modelBuyer = bll.GetModel(thisID);
                int thisUserID = modelBuyer.UserID;
                bllUser.Delete(thisUserID);
                bll.Delete(thisID);
                //
                BLL.User_Operate_Log.Add(Security.GetCurrentUserID(), EnumHelper.TableName.Supplier, thisID, EnumHelper.OperateType.Delete, "");
                BLL.User_Operate_Log.Add(Security.GetCurrentUserID(), EnumHelper.TableName.PAUser, thisUserID, EnumHelper.OperateType.Delete, "");
            }
        }

    }
}